package edu.nf.api.controller.order;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.google.gson.Gson;
import edu.nf.api.controller.BaseController;
import edu.nf.util.alipay.OrderUtile;
import edu.nf.waimai.dao.order.OrderDAO;
import edu.nf.waimai.dao.order.impl.OrderDAOImpl;
import edu.nf.waimai.dao.user.ShoppingDAO;
import edu.nf.waimai.dao.user.impl.ShopPingDAOImpl;
import edu.nf.waimai.model.order.Order;
import edu.nf.waimai.model.order.UserOrder;
import edu.nf.waimai.model.shop.Shop;
import edu.nf.waimai.service.order.OrderService;
import edu.nf.waimai.service.order.impl.OrderServiceImpl;
import edu.nf.waimai.service.user.ShoppingService;
import edu.nf.waimai.service.user.impl.ShoppingServiceImpl;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.ForwardView;
import org.nf.web.servlet.view.JsonView;
import org.nf.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import static edu.nf.api.controller.alipay.config.ZfbPay.*;


/**
 * @Date 2023/6/19
 * @Author zgm
 * 订单
 */
public class OrderController extends BaseController {
    // 根据订单id查询
    @RequestMapping("/order/getShopOrder")
    public View getShopOrder(@RequestParam("orderId") String orderId){
        OrderDAO dao = new OrderDAOImpl();
        UserOrder order = dao.getShopOrder(orderId);
        return new JsonView(success(order));
    }
    // 查询商家订单
    @RequestMapping("/shop/Order/listShopOrder")
    public View listShopOrder(@RequestParam("shopId") Integer shopId,
                              @RequestParam("status") Integer orderStatus){
        return new JsonView(success(new OrderDAOImpl().listShopOrder(shopId,orderStatus)));
    }
    // 查询骑手已接的订单
    @RequestMapping("/shop/Order/listShopIsRiderOrder")
    public View listShopOrder1(@RequestParam("shopId") Integer shopId,
                              @RequestParam("status") Integer orderStatus){
        return new JsonView(success(new OrderDAOImpl().listShopIsRiderOrder(shopId,orderStatus)));
    }

    @RequestMapping("/user/Order/listOrder")
    public View listUserOrder(@RequestParam("uid") Integer uid,
                              @RequestParam("orderType") String orderType){
        OrderService service = new OrderServiceImpl();
        List<UserOrder> list = service.queryOrder(uid, orderType);
        return new JsonView(success(list));
    }
    // 编辑商家订单状态
    @RequestMapping("/shop/Order/updateShopOrderStatus")
    public View updateShopOrderStatus(@RequestParam("orderId")String orderId,@RequestParam("status")Integer status){
        OrderDAO orderDAO = new OrderDAOImpl();
        int row = orderDAO.updateOrder(orderId, status);

        return row >= 1 ? new JsonView(success()) : new JsonView(error("error"));
    }
    @RequestMapping("/Order/ConfirmPayment")
    public View addOrder(@Model Order order, HttpSession session){
        OrderService service = new OrderServiceImpl();
        order.setOrderId(OrderUtile.orderNo());
        service.addOrder(order);
        session.setAttribute("order", order);
        return new JsonView(success());
    }
    @RequestMapping("/Order/updateOrderStatus")
    public View updateOrderStatus(HttpSession session){
        Order order = (Order)session.getAttribute("order");
        System.out.println(order);
        OrderDAO orderDAO = new OrderDAOImpl();
        orderDAO.updateOrder(order.getOrderId(), 1);
        ShoppingDAO dao = new ShopPingDAOImpl();
        dao.updateShopPing(order.getUid(), order.getShopId());

        return new RedirectView("http://localhost:9090/orderDetails");
    }
    @RequestMapping("/user/Order/getUserOrder")
    public View getUserOrder(@RequestParam("uid") Integer uid){
        OrderDAO dao = new OrderDAOImpl();
        UserOrder order = dao.getUserOrder(uid);
        return new JsonView(success(order));
    }
    @RequestMapping("/alipay/payApp")
    public String sss(@RequestParam("total") String total,HttpServletResponse resp , HttpServletRequest req ,HttpSession session){
        String zbf = null;
        try {
            zbf = zbf(total,resp,req,session);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        return zbf;
    }



    public static String zbf(String sum, HttpServletResponse resp , HttpServletRequest req,HttpSession session) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(URL);
        alipayConfig.setAppId(APPID);
        alipayConfig.setPrivateKey(APP_PRIVATE_KEY);
        alipayConfig.setFormat(FORMAT);
        alipayConfig.setAlipayPublicKey(ALIPAY_PUBLIC_KEY)  ;
        alipayConfig.setCharset(CHARSET);
        alipayConfig.setSignType(RSA2);
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        /*订单号*/
        model.setOutTradeNo(OrderUtile.orderNo());
        /*订单总金额*///
        model.setTotalAmount(String.valueOf(sum));
        /*订单标题*/
        model.setSubject("WaiMai");
        /*订单*/
        model.setProductCode("QUICK_WAP_WAY");
        /*设置完成后地址*/
        model.setPromoParams("");

        /*通过传过来的Id查询订单数据*/

        /*创建商品*/
        List<GoodsDetail> goodsDetail=new ArrayList<GoodsDetail>();
        /*设置商品*/
        GoodsDetail goodsDetail1=new GoodsDetail();

        /*加入list*/
        goodsDetail.add(goodsDetail1);

        /*设置到model中*/
        model.setGoodsDetail(goodsDetail);



        request.setBizModel(model);
        request.setReturnUrl("http://localhost:8081/Order/updateOrderStatus");

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);

        //输出
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }
}